package com.litchishell.easy;

/**
 * @author LitchiShell
 * @description: 题目26：删除有序数组中的重复项
 * @create 2022-08-21
 */
public class Solution007 {

    // 常规题解
    public static int removeDuplicates(int[] nums) {
        if (nums == null || nums.length < 0) {
            return -1;
        }
        int before = 0;
        int after = 1;
        while (after < nums.length) {
            if (nums[before] == nums[after]) {
                after++;
            } else {
                before++;
                // 直接将后面不重复的值进行赋值
                nums[before] = nums[after];
                after++;
            }
        }
        return before + 1;
    }

    // 官方题解
    public int removeDuplicatesCopy(int[] nums) {
        int n = nums.length;
        if (n == 0) {
            return 0;
        }
        int fast = 1, slow = 1;
        while (fast < n) {
            if (nums[fast] != nums[fast - 1]) {
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,2,3,3,4,5,6};
        System.out.println(removeDuplicates(nums));
    }
}
